# 专门解析ouput输出的json文件，输出统计量
import json
import re
from numpy import *
import numpy as np
import matplotlib.pyplot as plt

'''
    2022_05_11_23_05_datas.json 单路视频 OS调度 1 1 1
    2022_05_12_00_20_datas.json 单路视频 SmartPipe调度 1 1 1
    2022_05_13_01_19_datas.json 单路视频 SmartPipe调度 自适应调整部署后 1 1 1 

    2022_05_13_21_13_datas.json 两路视频 Os调度 1 1 1
    2022_05_13_21_23_datas.json 两路视频 SmartPipe调度 1 1 1
'''
# 将耗时存储到字典中{name:[]}]

# 读取json文件
with open("./lab1/output/2022_05_13_01_19_datas.json", "r") as json_file:
    json_dict = json.load(json_file)
    print("items: ", len(json_dict))

# 输出总耗时
start_time = json_dict[0]['ts']
end_time = json_dict[-1]['ts'] + json_dict[-1]['dur']
print("time cost: ", (end_time - start_time)/1000/1000, "s")

# 提取每个request的延迟
start = []
end = []
for i in json_dict:
    if "genFromMemory" in i['name']:
        start.append(i['ts'])
    if "markAndSave" in i['name']:
        end.append(i['ts'] + i['dur'])

# 平均延迟和延迟方差
print(len(start), len(end))
durs = []
for i in range(min(len(start), len(end))):
    durs.append(end[i] - start[i])

avg_delay = mean(durs)
print("平均请求延迟：", round(avg_delay/1000/1000, 3), "s")
std_delay = std(durs)
print("请求标准差：", round(std_delay/1000/1000, 3), "s")

'''
    Executor
'''

# 按照Executor(tid)进行信息提取
data = {}
for i in json_dict:
    if i['tid'] not in data:
        data[i['tid']] = [i['dur']]
    else:
        data[i['tid']].append(i['dur'])

# 输出处理次数，处理时间和，以及处理时间均值
print("Executors:")
index = 0
for x in data.keys():
    print(x, " count: ", len(data[x]), " sum: ", round(sum(data[x])/1000,3), "ms avg: ", round(sum(data[x])/len(data[x])/1000, 3), "ms")
    index += 1

# 绘制条形图
x = []
y_sum = []
for i in data.keys():
    x.append(int(i))
    y_sum.append(round(sum(data[i])/1000,3))

# 输出Executor占用耗时图
fig, ax = plt.subplots(figsize=(10, 7))
ax.bar(x=x, height=y_sum)
plt.xlabel('executors')
plt.ylabel('time (ms)')
plt.title('SmartPipe 1 channel adjust 1 1 1')
plt.savefig('Executor.jpg')

'''
    Function
'''
# 按照Function进行信息提取
data = {}
for i in json_dict:
    name = i['name'].split(':')[0]
    if name not in data:
        data[name] = [i['dur']]
    else:
        data[name].append(i['dur'])

# 输出处理次数，处理时间和，以及处理时间均值
print("Functions:")
index = 0
for x in data.keys():
    print(x, " count: ", len(data[x]), " sum: ", round(sum(data[x])/1000,3), "ms avg: ", round(sum(data[x])/len(data[x])/1000, 3), "ms")
    index += 1

# 绘制条形图
x = range(len(data.keys()))
y_sum = []
for i in data.keys():
    y_sum.append(round(sum(data[i])/1000,3))

# 输出Function占用耗时图
fig, ax = plt.subplots(figsize=(10, 7))
ax.bar(x=x, height=y_sum)
plt.xlabel('functions')
plt.ylabel('time (ms)')
plt.title('SmartPipe 1 channel adjust 1 1 1')
plt.savefig('Function.jpg')